import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {getToken} from "../../plugins/cookie";

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/',
        name: 'Layout',
        component: () => import('../views/Layout.vue'),
        children: [
            {
                path: 'task',
                name: 'Task',
                component: () => import('../views/task/TaskLayout.vue'),
                redirect: { name: 'Activity' },
                children: [
                    {
                        path: 'activity',
                        name: 'Activity',
                        component: () => import('../views/task/Activity.vue'),
                        redirect: { name: 'ActivityList' },
                        children: [
                            {
                                path: 'list',
                                name: 'ActivityList',
                                component: () => import('../views/task/ActivityList.vue'),
                            },{
                                path: 'create',
                                name: 'ActivityCreate',
                                component: () => import('../views/task/ActivityCreate.vue'),
                            },
                        ]
                    },
                    {
                        path: 'promo',
                        name: 'Promo',
                        component: () => import('../views/task/Promo.vue')
                    },
                    {
                        path: 'stat',
                        name: 'Stat',
                        component: () => import('../views/task/Stat.vue')
                    },
                    {
                        path: 'fans',
                        name: 'Fans',
                        component: () => import('../views/task/Fans.vue')
                    }
                ]
            },
            {
                path: 'msg',
                name: 'Msg',
                component: () => import('../views/msg/MsgLayout.vue'),
                children: [
                    {
                        path: 'push',
                        name: 'Push',
                        component: () => import('../views/msg/Push.vue')
                    },
                    {
                        path: 'sop',
                        name: 'Sop',
                        component: () => import('../views/msg/Sop.vue')
                    }
                ]
            },
            {
                path: 'auth',
                name: 'Auth',
                component: () => import('../views/auth/Auth.vue')
            }
        ]
    },
    {
        path: '/test',
        name: 'test',
        component: () => import('../views/text.vue')
    },
    {
        path: '/',
        name: 'home',
        component: HomeView
    },
    {
        path: '/about',
        name: 'about',
        component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
    let token=getToken();
    // 如果已登录
    if (token){
        next();
        return;
    }
    // 未登录 访问登录页面
    if(to.name==="Login"){
        next();
        return;
    }
    // 未登录，访问非登录页面，跳转回登录界面
    next({name: 'Login'})
})

export default router
